import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)
        
    def test1(self):
        self.browser.get('http://likeshop.abc/admin/account/login.html')
        sleep(2)
        self.browser.find_element(By.NAME,'account').send_keys('admin')
        self.browser.find_element(By.NAME,'password').send_keys('123456')
        self.browser.find_element(By.ID,'login').click()
        sleep(2)
        #定位商品标签并点击商品
        self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[2]/div/ul/li[1]/a/cite').click()
        sleep(2)
        #定位子框架
        iframe=self.browser.find_element(By.XPATH,'/html/body/div/div[3]/div[2]/iframe')
        #切换子框架
        self.browser.switch_to.frame(iframe)
        #定位查询商品名称并输入文具
        self.browser.find_element(By.NAME,'keyword').send_keys('文具')
        #定位查询按钮并点击查询
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]').click()
        sleep(2)

        #接受返回的商品列表
        #定位商品列表
        is_not_bug =True
        product_list=self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]')
        if len(product_list)==0:
            print("没有查询到商品")
        else:
            #遍历商品列表
            for product in product_list:
                #定位商品名称
                product_name =product.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[1]/table/thead/tr/th[2]/div/span').text
                if '文具' not in product_name:
                    print("有BUG")
        if is_not_bug:
            print("没有BUG")
            
        #退出子框架
        self.browser.switch_to.default_content()
        #定位用户信息
        dt= self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[1]/ul[2]/li[3]/a/cite')
        #鼠标悬停在用户信息上
        ActionChains(self.browser).move_to_element(dt).perform()
        sleep(2)
        #定位退出按钮并点击退出
        self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        sleep(2)

if __name__ == '__main__':
    lk=LikeShopTest()
    lk.test1()
